<?php

require_once '../api/engine/Template.php';
require_once 'style.php';
require_once 'blog_list.php';

use PolarisbackEndFrameRe\api\engine\Template;

class MainPageRender {

    public function __construct(){
        $main = file_get_contents("template/MainPage.html");
        $template = new Template($main);
        $template->assign('title', value: style()['title']);
        $template->assign('website_top_title', value: style()['website_top_title']);
        $template->assign('header_color', value: style()['style']['header_color']);
        $template->assign('button_color', value: style()['style']['button_color']);
        $template->assign('side_bar_color', value: style()['style']['side_bar_color']);
        if (style()['style']['side_bar_color'] == 'glass'){
            $template->assign('side_bar_color', value: '');
        }
        if (style()['enabled_sidebar'] == "on"){
            $template->assign("enabled_sidebar", '
          <div class="sidebar">
                {{ side_bar }}
            </div>');
        } elseif (style()['enabled_sidebar'] == "off") {
            $template->assign("enabled_sidebar", '');
        }

        if (style()['side_bar_status'] == "on"){
            $template->assign("side_bar", '  
              <h2>导航 <button class="close-btn" onclick="toggleSidebar()">&times;</button></h2>
                    <ul>
                        {{ blog_list }}
                    </ul>');
        } elseif (style()['side_bar_status'] == "off") {
            $template->assign("side_bar", '');
        }

        $template->assign('text_color', value: style()['style']['text_color']);
        $template->assign('close_button_color', value: style()['style']['close_button_color']);
        $template->assign(name: 'close_button_hover_color', value: style()['style']['close_button_hover_color']);
        $template->assign(name: 'side_bar_button_hover_color', value: style()['style']['side_bar_button_hover_color']);
        $template->assign(name: 'news', value: style()['style']['news']);

        // 生成 blog_list
        $blogListHtml = '';
        foreach (LISTS as $LIST){
            $link = LINKS[$LIST] ?? '#'; // 如果没有对应的链接，默认使用 #
            $blogListHtml .= "<li><a href='$link'>$LIST</a></li>";
        }
        $template->assign(name: 'blog_list', value: $blogListHtml);

        // 生成 articles_list
        $articles = [];
        $articleDir = "template/articles_save";
        foreach (glob($articleDir . "/*.html") as $file){
            $fileName = basename($file, ".html");
            // 打开文件，将<title></title>包裹住的内容提取出来
            $content = file_get_contents($file);
            preg_match('/<title>(.*?)<\/title>/', $content, $matches);
            $title = $matches[1] ?? '';

            // 检查文件名是否包含时间戳和随机字符串
            if (preg_match('/^\d+_[a-zA-Z]+$/', $fileName)) {
                // 如果是随机生成的文件名，使用文件内容中的标题
                $fileName = rawurlencode($fileName);
            } else {
                // 检查标题是否为中文
                if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $title)) {
                    // 如果是中文，对链接进行编码
                    $fileName = rawurlencode($fileName);
                }
            }

            $articles[] = ['title' => $title, 'link' => "/article/$fileName"];
        }

        // 分页逻辑
        $articlesPerPage = 10000000;
        $currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
        $totalPages = ceil(count($articles) / $articlesPerPage);

        $start = ($currentPage - 1) * $articlesPerPage;
        $end = $start + $articlesPerPage;
        $paginatedArticles = array_slice($articles, $start, $articlesPerPage);

        $articlesListHtml = '';
        foreach ($paginatedArticles as $article) {
            $articlesListHtml .= "<li><a href='{$article['link']}'>{$article['title']}</a></li>";
        }
        $template->assign(name: 'articles_list', value: $articlesListHtml);

        echo $template->render();
    }
}

new MainPageRender();
